#!/bin/sh

case "$1" in
  start)
    if crontab -l | grep -q "#APPINIT_NewzBook#";
    then
      echo "It looks like the NewzBook entry already exists."
      exit 0
    else
      echo "The NewzBook entry doesn't appear to exist in cron. We will attempt to add it."
      
      #echo "Getting the current cron entries and putting them in a temporary file"
      crontab -l > /tmp/cronentries.a
      
      #echo "Adding our NewzBook entry to the temporary file..."
      echo "*/30 * * * * /share/Apps/NewzBook//crontab.sh #APPINIT_NewzBook#" >> /tmp/cronentries.a
      
      #echo "Replacing the old cron entries with the contents of the temporary file..."
      crontab /tmp/cronentries.a
      
      #echo "Removing the temporary file..."
      rm -f /tmp/cronentries.a
      
      if crontab -l | grep -q "#APPINIT_NewzBook#";
      then
        echo "NewzBook has been successfully added as a cron entry!";
        exit 0
      else
        echo "ERROR: Newzbook could not be added as a cron entry.";
        exit 1
      fi
    fi

    # this function can be used to comment out the line 
    # crontab -l |sed -e 's=\(^.*#APPINIT_NewzBook#$\)=#\1=' | crontab -

    # this function can be used to uncomment the line
    #crontab -l | sed -e 's=^#\(.*#APPINIT_NewzBook#$\)=\1=' | crontab -
    
    ;;
  *)
    echo "Usage: $0 {start|stop}"
    exit 1
esac

exit 0  
